package com.taiyi.renaissancemall.ui.mine.address.add

import android.os.Bundle
import android.util.Log
import cn.qqtheme.framework.picker.SinglePicker
import com.lljjcoder.Interface.OnCityItemClickListener
import com.lljjcoder.bean.CityBean
import com.lljjcoder.bean.DistrictBean
import com.lljjcoder.style.cityjd.JDCityConfig
import com.lljjcoder.style.cityjd.JDCityPicker
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.Utils.ToastUtils
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.bean.AddressInfoBean
import com.taiyi.renaissancemall.bean.ProvinceBean
import kotlinx.android.synthetic.main.activity_add_address.*
import kotlinx.android.synthetic.main.base_top_black_background_layout.*

class AddAddressActivity : BaseActivity<AddAddressView, AddAddressPresent>(), AddAddressView {

    var provinceList = mutableListOf<ProvinceBean>()
    var cityList = mutableListOf<ProvinceBean>()
    var areaList = mutableListOf<ProvinceBean>()
    var provinceName: String = ""
    var cityName: String = ""
    var districtName: String = ""
    var provinceId: String = ""
    var cityId: String = ""
    var districtId: String = ""
    var source = 0


    override fun getLayoutId(): Int = R.layout.activity_add_address

    override fun initPresenter(): AddAddressPresent = AddAddressPresent()

    override fun initViews(savedInstanceState: Bundle?) {
        titleTv.text = getString(R.string.addNewAddress)
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(top).statusBarDarkFont(false, 1.0f).init()
    }

    override fun initData() {
        if (intent.getStringExtra("addressId") != null) {//编辑地址
            source = 1
            presenter.getAddressInfo()
        }
        presenter.getAddressLevelList("0", 1)//获取省份列表
    }

    override fun initEvent() {
        backIv.setOnClickListener { finish() }
        defaultAddressTv.setOnClickListener {
            defaultAddressTv.isSelected = !defaultAddressTv.isSelected
        }
        receiveAddressTv.setOnClickListener {
            val cityPicker = JDCityPicker()
            val jdCityConfig = JDCityConfig.Builder().build()

            jdCityConfig.showType = JDCityConfig.ShowType.PRO_CITY_DIS
            cityPicker.init(this)
            cityPicker.setConfig(jdCityConfig)
            cityPicker.setOnCityItemClickListener(object : OnCityItemClickListener() {
                override fun onSelected(
                    province: com.lljjcoder.bean.ProvinceBean?,
                    city: CityBean?,
                    district: DistrictBean?
                ) {
                    receiveAddressTv.text = province?.name + city?.name + district?.name
                    provinceName = province?.name!!
                    cityName = city?.name!!
                    districtName = district?.name!!

                    for (index in provinceList.indices) {
                        var province1 = provinceList[index]
                        if (province1.area_name == provinceName) {
                            Log.i("ddddddddddddd", "-------" + province1.area_id)
                            provinceId = province1.area_id
                            presenter.getAddressLevelList(provinceId, 2)
                            break
                        }
                    }
                }

                override fun onCancel() {}
            })

            cityPicker.showCityPicker()
        }
        saveTv.setOnClickListener {
            var name = receiverNameEt.text.toString()
            if (name.isEmpty()) {
                ToastUtils.showLongToast("请输入收货人姓名")
                return@setOnClickListener
            }
            var phoneNumber = phoneNumberEt.text.toString()
            if (phoneNumber.isEmpty()) {
                ToastUtils.showLongToast("请输入手机号")
                return@setOnClickListener
            }
            var address = remarkAddressEt.text.toString()
            if (address.isEmpty()) {
                ToastUtils.showLongToast("请输入详细地址")
                return@setOnClickListener
            }
            if (provinceId == "") {
                ToastUtils.showLongToast("请选择省份")
                return@setOnClickListener
            }
            if (cityId == "") {
                ToastUtils.showLongToast("请选择城市")
                return@setOnClickListener
            }
            if (districtId == "") {
                ToastUtils.showLongToast("请选择区县")
                return@setOnClickListener
            }

            if (source == 0) {
                showProgress()
                presenter.addAddress()
            } else {
                showProgress()
                presenter.updateAddress()
            }
        }


        privinceTv.setOnClickListener {
            showSelector(1)
        }
        cityTv.setOnClickListener {
            showSelector(2)
        }
        areaTv.setOnClickListener {
            showSelector(3)
        }
    }

    fun showSelector(type: Int) {

        var list = mutableListOf<String>()
        if (type == 1) {
            for (item in provinceList) {
                list.add(item.area_name)
            }
        } else if (type == 2) {
            for (item in cityList) {
                list.add(item.area_name)
            }
        } else if (type == 3) {
            for (item in areaList) {
                list.add(item.area_name)
            }
        }
        var singlePicker = SinglePicker<String>(this, list)
        singlePicker.setOnItemPickListener { index, item ->
            when (type) {
                1 -> {
                    provinceId = provinceList[index].area_id
                    privinceTv.text = provinceList[index].area_name
                    cityTv.text = "请选择市"
                    cityId = ""
                    areaTv.text = "请选择区县"
                    districtId = ""
                    showProgress()
                    presenter.getAddressLevelList(provinceId, 2)
                }
                2 -> {
                    cityId = cityList[index].area_id
                    cityTv.text = cityList[index].area_name
                    areaTv.text = "请选择区县"
                    districtId = ""
                    showProgress()
                    presenter.getAddressLevelList(cityId, 3)
                }
                3 -> {
                    districtId = areaList[index].area_id
                    areaTv.text = areaList[index].area_name
                }
            }

        }
        singlePicker.show()
    }

    override fun getSelCityId(): String = cityId
    override fun addAddressFailure(msg: String?) {
        hideProgress()
    }

    override fun getProvinceListSuccess(list: MutableList<ProvinceBean>?, type: Int) {
        hideProgress()
        if (type == 1) {
            provinceList.clear()
            for (item in list!!) {
                var item1 = item
                provinceList.add(item1)
            }
        } else if (type == 2) {
            cityList.clear()
            for (item in list!!) {
                var item1 = item
                cityList.add(item1)
            }
        } else if (type == 3) {
            areaList.clear()
            for (item in list!!) {
                var item1 = item
                areaList.add(item1)
            }
        }

    }

    override fun addAddressSuccess() {
        hideProgress()
        finish()
    }

    override fun getDetailAddress(): String = remarkAddressEt.text.toString()

    override fun getSelProvinceId(): String = provinceId
    override fun ifDefault(): String = when (defaultAddressTv.isSelected) {
        true -> "1"
        else -> "0"
    }

    override fun getPhoneNumber(): String = phoneNumberEt.text.toString()
    override fun getAddressInfoFailure(msg: String?) {
        toast(msg)
    }


    override fun getReceiverName(): String = receiverNameEt.text.toString()


    override fun getProvinceListFailure(msg: String?) {
        toast(msg)
    }

    override fun getAddressId(): String = intent.getStringExtra("addressId")

    override fun getAddressInfoSuccess(addressInfoBean: AddressInfoBean?) {
        provinceId = addressInfoBean?.provinceid.toString()
        cityId = addressInfoBean?.cityid.toString()
        districtId = addressInfoBean?.areaid.toString()
        presenter.getAddressLevelList(provinceId, 2)
        presenter.getAddressLevelList(cityId, 3)
        privinceTv.text = addressInfoBean?.provincename
        cityTv.text = addressInfoBean?.cityname
        areaTv.text = addressInfoBean?.areaname
        receiverNameEt.setText(addressInfoBean?.name)
        phoneNumberEt.setText(addressInfoBean?.tel)
        receiveAddressTv.text =
            addressInfoBean?.provincename + addressInfoBean?.cityname + addressInfoBean?.areaname
        remarkAddressEt.setText(addressInfoBean?.address)
    }

    override fun getSelDistrictId(): String = districtId


}


